// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// B [author] rhyme_lph
// u [email]  rhymelph@gmail.com
// g [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/provider/login/set_new_password_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:finance_app/src/widgets/login/login_top_screen_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

@RouterMeta(paramName: "setNewPasswordPage")
class SetNewPasswordPage extends StatefulWidget {
  final String phoneNumber;
  final String otp;
  final String countryCode;
  final String email;

  const SetNewPasswordPage({
    Key key,
    this.phoneNumber,
    this.otp,
    this.countryCode,
    this.email,
  }) : super(key: key);

  @override
  _SetNewPasswordPageState createState() => _SetNewPasswordPageState();
}

class _SetNewPasswordPageState extends State<SetNewPasswordPage>
    with PagePresenter<SetNewPasswordPage, SetNewPasswordProvider> {
  @override
  void initState() {
    super.initState();
    provider.email = widget.email;
    provider.phoneNumber = widget.phoneNumber;
    provider.otp = widget.otp;
    provider.countryCode = widget.countryCode;
  }

  @override
  Widget get buildWidget => Scaffold(
        body: ListView(
          padding: EdgeInsets.zero,
          children: [
            _buildImageScreen(),
            AppSpaces.h35,
            _buildTipText(tip: S.current.please_enter_the_new_password),
            AppSpaces.h20,
            listBuilder<bool>(
                builder: (BuildContext context, bool value, Widget child) =>
                    InputField(
                      key: ValueKey("Set New Password"),
                      leading: SvgPicture.asset(
                        A.assetsImagesCreatePadlock2,
                        width: getDp(15),
                        height: getDp(15),
                      ),
                      tip: S.current.password,
                      isPassword: true,
                      hasError: value,
                      helpText: S.current.at_least_6_alphanumerical_characters,
                      controller: provider.passwordController,
                      focusNode: provider.passwordFocusNode,
                    ),
                selector: (_, __) => __.passwordError),
            AppSpaces.h30,
            listBuilder<bool>(
                builder: (BuildContext context, bool value, Widget child) =>
                    NormalButton(
                      title: S.current.update_password,
                      onTap: value == true ? provider.onUpdatePassword : null,
                    ),
                selector: (_, __) => __.enable),
            AppSpaces.h30,
          ],
        ),
      );

  Widget _buildImageScreen() => LoginTopScreenWidget(
        imagePath: A.assetsImagesGraphicForgotPassword,
        imageName: S.current.set_new_password,
      );

  Center _buildTipText({String tip}) {
    return Center(
      child: Text(
        tip,
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(18),
          fontWeight: FontWeight.w700,
        ),
      ),
    );
  }
}
